/**
 * Copyright © 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.spareparts.material.web;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.spareparts.material.entity.SparepartsDownFlow;
import com.jeeplus.modules.spareparts.material.service.SparepartsDownFlowService;

/**
 * 下线备件管理流程表单Controller
 * @author 王洋
 * @version 2020-10-16
 */
@RestController
@RequestMapping(value = "/spareparts/material/sparepartsDownFlow")
public class SparepartsDownFlowController extends BaseController {

	@Autowired
	private SparepartsDownFlowService sparepartsDownFlowService;

	@ModelAttribute
	public SparepartsDownFlow get(@RequestParam(required=false) String id) {
		SparepartsDownFlow entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = sparepartsDownFlowService.get(id);
		}
		if (entity == null){
			entity = new SparepartsDownFlow();
		}
		return entity;
	}

	/**
	 * 流程表单列表数据
	 */
	@GetMapping("list")
	public AjaxJson list(SparepartsDownFlow sparepartsDownFlow, HttpServletRequest request, HttpServletResponse response) {
		Page<SparepartsDownFlow> page = sparepartsDownFlowService.findPage(new Page<SparepartsDownFlow>(request, response), sparepartsDownFlow);
		return AjaxJson.success().put("page",page);
	}

	/**
	 * 根据Id获取流程表单数据
	 */
	@GetMapping("queryById")
	public AjaxJson queryById(SparepartsDownFlow sparepartsDownFlow) {
		return AjaxJson.success().put("sparepartsDownFlow", sparepartsDownFlow);
	}

	/**
	 * 保存流程表单
	 */
	@PostMapping("save")
	public AjaxJson save(SparepartsDownFlow sparepartsDownFlow, Model model) throws Exception{
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(sparepartsDownFlow);
		if (StringUtils.isNotBlank(errMsg)){
			return AjaxJson.error(errMsg);
		}
		//新增或编辑保存
		sparepartsDownFlowService.save(sparepartsDownFlow);//保存
		return AjaxJson.success("保存流程表单成功").put("businessTable", "spareparts_down_flow").put("businessId", sparepartsDownFlow.getId());
	}


	/**
	 * 批量删除流程表单
	 */
	@DeleteMapping("delete")
	public AjaxJson delete(String ids) {
		String idArray[] =ids.split(",");
		for(String id : idArray){
			sparepartsDownFlowService.delete(new SparepartsDownFlow(id));
		}
		return AjaxJson.success("删除流程表单成功");
	}


}